using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Comteck.Dto.Sys;
using Comteck.Extensions;
using Comteck.Ktt.Services.Sys;
using Comteck.Linq;
using Comteck.Localization;
using Comteck.Winforms;
using Comteck.Winforms.Forms;
using Comteck.Winforms.Infrastructure;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Base;
using Microsoft.Extensions.DependencyInjection;

namespace Comteck.Ktt.WinUI.Sys {
  /// <summary>
  /// 打印模板管理
  /// </summary>
  public partial class FrmPrintTemplateAdmin : FrmBase {

    #region fields

    private readonly IPrintTemplateService printTemplateService;
    private CKObservableCollection<PrintTemplateDto> mainDataList;

    #endregion

    #region constructs

    public FrmPrintTemplateAdmin(IPrintTemplateService printTemplateService) {
      this.printTemplateService = printTemplateService;
      InitializeComponent();
    }

    #endregion

    #region form load

    /// <summary>
    /// 窗体加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void FrmPrintTemplateAdmin_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }

#if DEBUG
      btnDelete.Visibility = BarItemVisibility.Always;
#endif

      gcMain.DataSource = mainDataList = new CKObservableCollection<PrintTemplateDto>(this);
      await BindHelper.BindMenuAuth(rilpMenuId);
    }

    #endregion

    #region search

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnSearch_ItemClick(object sender, ItemClickEventArgs e) {
      ajaxLoad.Show();
      var result = await printTemplateService.GetAllAsync();
      ajaxLoad.Hide();
      mainDataList.DataSource = result.ToList();
    }

    #endregion

    #region grid

    /// <summary>
    /// 焦点变化事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void gvMain_FocusedRowObjectChanged(object sender, FocusedRowObjectChangedEventArgs e) {
      if (!(gvMain.GetFocusedRow() is PrintTemplateDto model)) {
        gcDetail.DataSource = null;
      } else {
        ajaxLoad.Show();
        var result = await printTemplateService.GetDetailListByTemplateIdAsync(model.Id);
        gcDetail.DataSource = result;
        ajaxLoad.Hide();
      }
    }

    #endregion

    #region 新增

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnAdd_ItemClick(object sender, ItemClickEventArgs e) {
      var form = SysGlobal.ServiceProvider.GetRequiredService<FrmPrintTemplateAction>();
      form.AddOrUpdateAction = (entity) => gvMain.BindAfterInsert<PrintTemplateDto>(entity);
      form.Owner = this;
      form.ShowDialog();
    }

    /// <summary>
    /// 拷贝新增
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnAddCopy_ItemClick(object sender, ItemClickEventArgs e) {
      if (!(gvMain.GetFocusedRow() is PrintTemplateDto model)) { return; }

      var form = SysGlobal.ServiceProvider.GetRequiredService<FrmPrintTemplateAction>();
      form.AddOrUpdateAction = (entity) => gvMain.BindAfterInsert<PrintTemplateDto>(entity);
      form.IsCopy = true;
      form.EntityId = model.Id;
      form.Owner = this;
      form.ShowDialog();
    }

    #endregion

    #region 修改

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnEdit_ItemClick(object sender, ItemClickEventArgs e) {
      if (!(gvMain.GetFocusedRow() is PrintTemplateDto model)) { return; }

      var form = SysGlobal.ServiceProvider.GetRequiredService<FrmPrintTemplateAction>();
      form.EntityId = model.Id;
      form.AddOrUpdateAction = (entity) => {
        gvMain.BindAfterUpdate(model, entity);
        this.gvMain_FocusedRowObjectChanged(gvMain, null);
      };
      form.Owner = this;
      form.ShowDialog();
    }

    #endregion

    #region 删除

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnDelete_ItemClick(object sender, ItemClickEventArgs e) {
      if (!(gvMain.GetFocusedRow() is PrintTemplateDto model)) {
        return;
      }

      if (this.ShowMsg(BaseResources.Delete_Selected_Confirm, null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
        return;
      }

      ajaxLoad.Show();
      var result = await printTemplateService.DeleteAsync(model.Id);
      ajaxLoad.Hide();
      if (result.Code == 1) {
        mainDataList.Remove(model);
        this.ShowMsg(BaseResources.Delete_Success);
      } else {
        this.ShowMsg(BaseResources.Delete_Failed_Reason, result.Message);
      }
    }

    #endregion

    #region 生成报表文件

    /// <summary>
    /// 生成报表文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnCreateReportFile_ItemClick(object sender, ItemClickEventArgs e) {
      if (!(gvMain.GetFocusedRow() is PrintTemplateDto model)) {
        return;
      }
      var dlgSave = new SaveFileDialog();
      var fileName = model.Name;
      dlgSave.Title = "导出报表文件";// 对话框题目
      dlgSave.FileName = fileName;
      dlgSave.Filter = "报表文件|*.frx";// 过滤格式
      if (dlgSave.ShowDialog() == DialogResult.OK) {
        var path = dlgSave.FileName;
        //File.WriteAllText(path, model.Data, Encoding.UTF8);
        var encoding = new System.Text.UTF8Encoding(false);
        File.WriteAllText(path, model.Data, encoding);
        this.ShowMsg("导出成功");
      }
    }

    #endregion

    #region export

    /// <summary>
    /// 导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnExport_ItemClick(object sender, ItemClickEventArgs e) {
      gvMain.ExportDialog();
    }

    #endregion

    #region close

    /// <summary>
    /// 关闭
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnClose_ItemClick(object sender, ItemClickEventArgs e) {
      this.Close();
    }

    #endregion

  }
}
